﻿///////////////////////////////////////////////////////////////////////////////////
/// 文 件 名: ReimbursementExpensesControl.cs
/// 作    者: XXD
/// 创建日期: 2012/6/18 9:48:18
/// 
///---------------------------------修改记录--------------------------------------
/// 修 改 人:          
/// 修改日期:
/// 修改原因: 
//////////////////////////////////////////////////////////////////////////////////
namespace ZX.WebApp.WorkFlow.CustomForms
{
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI.WebControls;
    using DevExpress.Web.ASPxGridView;
    using ZX.BPM.Library;

    /// <summary>
    /// (借款，需求，报销)报销单控件
    /// </summary>
    public partial class ReimbursementExpensesControl : System.Web.UI.UserControl
    {
        #region 私有方法

        /// <summary>
        /// 设置只读
        /// </summary>
        /// <param name="isRead">是否只读</param>
        void SetReadonly(bool isRead)
        {
            if (isRead)
            {
                ((GridViewDataMemoColumn)gridReimbursementExpenses.Columns["colExplain"]).ReadOnly = isRead;
                ((GridViewDataTextColumn)gridReimbursementExpenses.Columns["colAmount"]).ReadOnly = isRead;
                ((GridViewDataTextColumn)gridReimbursementExpenses.Columns["colReimbursementContent"]).ReadOnly = isRead;
                ((GridViewDataDateColumn)gridReimbursementExpenses.Columns["colStartDate"]).ReadOnly = isRead;
                ((GridViewDataComboBoxColumn)gridReimbursementExpenses.Columns["colReimbursementCategory"]).ReadOnly = isRead;
                ((GridViewCommandColumn)gridReimbursementExpenses.Columns["colOperator"]).Visible = !isRead;
            }
        }

        void AddError(
            Dictionary<GridViewColumn, string> errors, 
            GridViewColumn column, 
            string errorText)
        {
            if (errors.ContainsKey(column)) return;
            errors[column] = errorText;
        }


        /// <summary>
        /// 获取字符参数
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        string GetStringFromQueryString(string key, string defaultValue)
        {
            string queryStringValue = HttpContext.Current.Request.QueryString[key];
            if (queryStringValue == null)
            {
                return defaultValue;
            }

            return queryStringValue;
        }

        #endregion

        #region 外部接口

        NeedsOfBorrowerEdit dataSource;
        /// <summary>
        /// 当前页面数据源
        /// </summary>
        public NeedsOfBorrowerEdit DataSource
        {
            get
            {
                return dataSource;
            }
            set
            {
                dataSource = value;
            }
        }


        bool readOnly = false;
        /// <summary>
        /// 只读
        /// </summary>
        public bool ReadOnly
        {
            set
            {
                SetReadonly(value);
                readOnly = value;
            }
            get
            {
                return readOnly;
            }
        }

        public void EndEdit()
        {
            if (gridReimbursementExpenses.IsEditing)
            {
                gridReimbursementExpenses.EndUpdate();
            }
        }
        #endregion

        #region 事件处理
        protected void grid_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
        {
            if (e.NewValues["ReimbursementCategory"] == null || string.IsNullOrEmpty(e.NewValues["ReimbursementCategory"].ToString()))
            {
                AddError(e.Errors, gridReimbursementExpenses.Columns["ReimbursementCategory"], "报销类别必须填写.");
            }

            if (e.NewValues["ReimbursementContent"] == null || string.IsNullOrEmpty(e.NewValues["ReimbursementContent"].ToString()))
            {
                AddError(e.Errors, gridReimbursementExpenses.Columns["ReimbursementContent"], "报销内容必须填写.");
            }

            decimal amount = 0;
            decimal.TryParse(e.NewValues["Amount"].ToString().ToString(), out amount);
            if (amount==0)
            {
                AddError(e.Errors, gridReimbursementExpenses.Columns["Amount"], "金额必须大于零.");
            }


            if (string.IsNullOrEmpty(e.RowError) && e.Errors.Count > 0)
            {
                e.RowError = "请修改所有错误后，再提交.";
            }
        }
     

        protected void ObjectDataSource1_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = dataSource;
        }

        protected void grid_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
        {
            if (!gridReimbursementExpenses.IsNewRowEditing)
            {
                gridReimbursementExpenses.DoRowValidation();
            }
        }
        protected void grid_ParseValue(object sender, DevExpress.Web.Data.ASPxParseValueEventArgs e)
        {
            if (e.FieldName.Equals("Amount"))
            {
                decimal amount = 0;
                if (e.Value == null || !decimal.TryParse(e.Value.ToString(), out amount))
                {
                    throw new Exception("金额填写不正确。");
                }
            }
        }

        #endregion
    }
}